<template>
  <div>
    <el-form ref="teamRow" :rules="rules" :model="teamRow" label-width="110px">
      <el-row>
        <el-col class="singleDatePicker">
          <el-form-item prop="projectName" label="项目名称" size="small">
            <el-input type="text" v-model="teamRow.projectName"></el-input>
          </el-form-item>
          <el-form-item label="入账团队" size="small">
            <!--<el-autocomplete-->
            <!--v-model="teamRow.teamName"-->
            <!--:fetch-suggestions="querySearchTeam"-->
            <!--@select="handleSelectTeam"-->
            <!--placeholder="请输入内容"-->
            <!--&gt;-->
            <!--</el-autocomplete>-->
            <el-input type="text" v-model="teamRow.teamName" disabled></el-input>
          </el-form-item>
          <el-form-item label="入账金额" size="small">
            <el-input type="number" v-model.number="teamRow.amount">
              <span slot="suffix">万元</span>
            </el-input>
          </el-form-item>
          <el-form-item prop="date" label="入账时间" size="small">
            <el-date-picker
              v-model="teamRow.date"
              type="date"
              value-format="timestamp"
              placeholder="选择日期"
              style="width: 190px;"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="经办人" size="small">
            <el-autocomplete
              v-model="teamRow.agent"
              :fetch-suggestions="querySearchOperator"
              placeholder="请输入内容"
              style="width: 190px;"
            >
            </el-autocomplete>
          </el-form-item>
          <el-form-item label="入账凭证" size="small">
            <input type="file" ref='credentials' @change="getFile($event)">
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="备注说明" size="small">
        <el-input type="textarea" v-model="teamRow.remark"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="warning" size="small"  @click="submitForm('teamRow')" v-no-more-click>确定</el-button>
      <el-button size="small" @click="$emit('closeDialog')">取消</el-button>
    </div>
  </div>
</template>

<script>
  import {
    getList,
    selectCurrentAccount,
    addCurrentAccount,
    updateCurrentAccount,
    deleteCurrentAccount,
    selectTeam,
    selectOperator
  } from '@/api/currentaccount'
  import myTable from '@/components/myTable'

  import { AreaSelect } from 'vue-area-linkage'
  import { pca } from 'area-data'
  import { formatDate } from '@/utils/index'

  const time = {
    props: ['colConfig'],
    filters: {
      formatDate(time) {
        var date = new Date(time)
        return formatDate(date, 'yyyy-MM-dd')
      }
    },
    template: `
  <el-table-column :label="colConfig.label" :width="colConfig.width">
      <span slot-scope="scope">
        {{ scope.row[colConfig.prop] | formatDate }}
      </span>
  </el-table-column>
  `
  }

  const credentials = {
    props: ['colConfig'],
    methods: {
      openVisible(label, src) {
        const msg = {
          label: label,
          src: src
        }
        this.$emit('openImgVisible', msg)
      }
    },
    filters: {},
    template: `
  <el-table-column :label="colConfig.label" :width="colConfig.width">
      <span slot-scope="scope">
        <a v-if="scope.row[colConfig.prop]" @click="openVisible(colConfig.label,scope.row[colConfig.prop])">查看</a>
      </span>
  </el-table-column>
  `
  }

  export default {
    components: {
      myTable,
      AreaSelect
    },
    props: ['outerVisible', 'balanceId'],
    data() {
      return {
        colConfigs: [
          { prop: 'projectName', label: '项目名称', minWidth: '35%', fixed: true },
          { prop: 'teamName', label: '入账团队', minWidth: '10%' },
          { prop: 'amount', label: '入账金额(万元)', width: '100' },
          { prop: 'date', label: '入账时间', width: '100', component: time },
          { prop: 'agent', label: '经办人', width: '100' },
          { prop: 'credentials', label: '入账凭证', width: '80', component: credentials },
          { prop: 'remark', label: '备注说明', minWidth: '20%' },
          { slot: 'opt' }
        ],
        searchForm: {},
        searchFormList: {},
        costTypes: [],
        typeNames: [],
        listLoading: true,
        dialogTitle: '新增往来账',
        dialogType: 1,
        teamRow: {},
        teamRowClear: {},
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        total: 1,
        innerVisible: false,
        pca: pca,
        rules: {
          projectName: [
            { required: true, message: '请输入项目名称', trigger: 'change' }
          ],
          teamName: [
            { required: true, message: '请输入入账团队', trigger: 'change' }
          ],
          date: [
            { required: true, message: '请选择日期', trigger: 'change' }
          ]
        },
        visible: false,
        imgVisible: false,
        imgMsg: {}
      }
    },
    created() {
      // this.teamRowClear = JSON.parse(JSON.stringify(this.teamRow))
      // this.fetchData()
    },
    computed: {
      teamId() {
        return this.$store.state.user.teamId
      },
      teamName() {
        return this.$store.state.user.teamName
      }
    },
    methods: {
      fetchData() {
        this.searchFormList.pageNum = this.pageNum
        this.searchFormList.pageSize = this.pageSize
        this.listLoading = true
        getList(this.searchFormList).then(response => {
          this.tableData = response.responseBody.list
          this.total = response.responseBody.total
          console.log(this.tableData)
          this.listLoading = false
        })
      },
      search() {
        this.pageNum = 1
        this.searchFormList = JSON.parse(JSON.stringify(this.searchForm))
        this.fetchData()
      },
      addTeam() {
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.dialogType = 1
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        if (this.$refs.credentials) {
          this.$refs.credentials.value = ''
        }
      },
      editTeam(currentAccountId) {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '修改往来账'
        this.dialogType = 0
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        if (this.$refs.credentials) {
          this.$refs.credentials.value = ''
        }
        const params = {
          currentAccountId: currentAccountId
        }
        selectCurrentAccount(params).then(response => {
          this.teamRow = Object.assign({}, response.responseBody)
        })
      },
      submitForm(formName) {
        const params = new FormData()
        params.append('projectName', this.teamRow.projectName)
        params.append('teamId', this.teamRow.teamId)
        params.append('teamName', this.teamRow.teamName)
        params.append('amount', this.teamRow.amount || 0)
        params.append('date', this.teamRow.date)
        params.append('agent', this.teamRow.agent || '')
        params.append('remark', this.teamRow.remark || '')
        params.append('file', this.teamRow.credentials)
        if (!this.dialogType) {
          params.append('currentAccountId', this.teamRow.currentAccountId)
        }
        // 判断balanceId 银行余额登记 与原页面不同
        if (this.balanceId) {
          params.append('balanceId', this.balanceId)
        }
        if (this.teamRow.teamId) {
          this.$refs[formName].validate((valid) => {
            if (valid) {
              if (this.dialogType) {
                addCurrentAccount(params).then(response => {
                  this.addTeam()
                })
              } else {
                updateCurrentAccount(params).then(response => {
                  this.addTeam()
                })
              }
            } else {
              console.log('error submit!!')
              return false
            }
          })
        } else {
          this.$message({
            message: '该入账团队不存在',
            type: 'warning'
          })
        }
      },
      deleteRow(scope) {
        this.$refs[`popover-${scope.$index}`].doClose()
        const params = {
          currentAccountId: scope.row.currentAccountId,
          date: scope.row.date,
          teamId: scope.row.teamId,
          amount: scope.row.amount,
          isDel: 1
        }
        deleteCurrentAccount(params).then(response => {
          this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      },
      getFile(event) {
        this.teamRow.credentials = event.target.files[0]
      },
      openVisible(msg) {
        this.imgVisible = true
        this.imgMsg = msg
      },
      querySearchOperator(queryString, callback) {
        // queryString 为在框中输入的值
        // callback 回调函数,将处理好的数据推回
        if (queryString) {
          const params = {
            teamId: this.teamRow.teamId,
            name: queryString
          }
          selectOperator(params).then(response => {
            for (const i of response.responseBody) {
              i.value = i.name
            }
            let projectList = response.responseBody
            if (projectList.length === 0) {
              projectList = [{ value: '暂无数据' }]
            }
            callback(projectList)
          })
        } else {
          callback([])
        }
      },
      querySearchFormTeam(queryString, callback) {
        // queryString 为在框中输入的值
        // callback 回调函数,将处理好的数据推回
        delete this.teamRow.teamId
        if (queryString) {
          const params = {
            teamName: queryString
          }
          selectTeam(params).then(response => {
            for (const i of response.responseBody) {
              i.value = i.teamName
            }
            let nameList = response.responseBody
            if (nameList.length === 0) {
              nameList = [{ value: '暂无数据' }]
            }
            callback(nameList)
          })
        } else {
          callback([])
        }
      },
      handleSelectFormTeam(item) {
        this.$set(this.searchForm, 'teamId', item.teamId)
        this.$set(this.searchForm, 'teamName', item.teamName)
      },
      querySearchTeam(queryString, callback) {
        // queryString 为在框中输入的值
        // callback 回调函数,将处理好的数据推回
        delete this.teamRow.teamId
        if (queryString) {
          const params = {
            teamName: queryString
          }
          selectTeam(params).then(response => {
            for (const i of response.responseBody) {
              i.value = i.teamName
            }
            let nameList = response.responseBody
            if (nameList.length === 0) {
              nameList = [{ value: '暂无数据' }]
            }
            callback(nameList)
          })
        } else {
          callback([])
        }
      },
      handleSelectTeam(item) {
        this.$set(this.teamRow, 'teamId', item.teamId)
        this.$set(this.teamRow, 'teamName', item.teamName)
      }
    },
    watch: {
      'outerVisible': {
        handler(newValue, oldValue) {
          this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
          if (this.$refs['teamRow']) {
            this.$refs['teamRow'].resetFields()
          }
          this.$set(this.teamRow, 'teamId', this.teamId)
          this.$set(this.teamRow, 'teamName', this.teamName)
        },
        immediate: true
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .dialog-footer {
    text-align: right;
  }

</style>


<style rel="stylesheet/scss" lang="scss">

</style>
